package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.request.AddFriendReq;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/4/6.
 */

public class SendMsgActivity extends BaseActivity implements View.OnClickListener{

    public static final String USERNAME = "Username";

    private EditText mEt;
    private Button mBtn;

    private String toAddUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toAddUsername = getIntent().getStringExtra(USERNAME);
        initView();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_addcontact;
    }

    void initView(){
        setTitleResId(R.string.icane_send_vd);

        mEt = (EditText)findViewById(R.id.icane_edit_vd_info);
        mBtn = (Button) findViewById(R.id.icane_send);

        mBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icane_send:
                final String str = mEt.getText().toString();
                if(TextUtils.isEmpty(str)){
                    ToastUtil.openBaseToast(this,R.string.send_vd_info_hint, Toast.LENGTH_SHORT);
                    return;
                }
                AddFriendReq req = new AddFriendReq();
                req.userId = toAddUsername;
                RequestManager.postAddFriend(mContext, req, new BaseRpCallback<BaseRopnse>() {
                    @Override
                    public BaseRopnse parseResponse(BaseRopnse response, int id) {
                        return response;
                    }

                    @Override
                    public void onResult(BaseRopnse o) {
                        try {
                            EMClient.getInstance().contactManager().addContact(toAddUsername, str);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFail(String errorMsg) {

                    }

                });

                finish();
                break;
        }
    }
}
